<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th><input type="checkbox" class="grid-select-all" />&nbsp;</th>
                    <th>SKU</th>
                    <th>产品名称</th>
                    <th>采购数量</th>
                    <th>已收货数量</th>
                    <th>现存公共库存</th>
                    <th>出库数量</th>
                </tr>
                </thead>
                <tbody>
                @foreach($purchase->order_detail as $detail)
                    @php
                        $inventory = \App\Models\Inventory\PublicCn::getInstant($detail->psku_id);
                    @endphp
                    <tr>
                        <td><input type="checkbox" class="grid-row-checkbox" data-id="{{ $detail->id }}" /></td>
                        <td>{{ $detail->sku->psku_code }}</td>
                        <td>{{ $detail->sku->psku_cn }}</td>
                        <td>{{ $detail->order_num ?? 0 }}</td>
                        <td>{{ $detail->rgoods_num ?? 0 }}</td>
                        <td>{{ $inventory }}</td>
                        <td>
                            <input type="hidden" name="detail_id" value="{{ $detail->id }}">
                            <input type="hidden" name="psku_id" value="{{ $detail->psku_id }}">
                            <input type="hidden" name="psku_code" value="{{ $detail->sku->psku_code ?? '' }}">
                            <input type="hidden" name="pack_price" value="{{ $detail->pack_price ?? 0 }}">
                            <input type="hidden" name="unit_price_no_tax" value="{{ numberFormat(\App\Models\Product\ProductSku::tax($detail->unit_price ?? 0,$tax ?: 0)) }}">
                            <input type="hidden" name="unit_price" value="{{ $detail->unit_price ?? 0 }}">
                            <input type="hidden" name="supplier_price" value="{{ $detail->supplier_price ?? 0 }}">
                            <input type="text" class="storage_num_"
                                   style="width: 100px; text-align: right;"
                                   name="storage_num"
                                   data-value="{{ min($detail->rgoods_num ?? 0,$inventory) }}"
                                   value="{{ min($detail->rgoods_num ?? 0,$inventory) }}">
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
<div style="margin-top: 10px;">
    <div class="box-body fields-group">
        <div class="form-group">
            <div class="col-sm-10" style="margin-left: 25%">
                <input type="hidden" name="order_no" value="{{ $purchase->purchase_no }}">
                <input type="hidden" name="supplier_id" value="{{ $purchase->supplier_id }}">
                <button type="button" class="btn btn-primary" id="receiving-out-submit">提交</button>
            </div>
        </div>
    </div>
</div>
<div class="box-body fields-group">
    <div class="form-group">
        <div class="col-sm-10" style="margin-left: 25%">
                <span style="color:red;">
                    <div>注意：</div>
                    <div>1.出库数量必须大于0</div>
                    <div>2.出库数量不能超过已收货数量和现存公共库存</div>
                    <div>3.已从公共分配到了站点的库存无法再参与采购出库</div>
                </span>
        </div>
    </div>
</div>
<script language="JavaScript" type="text/javascript">
    $(function () {
        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
            if (this.checked) {
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $(this).closest('tr').css('background-color', '');
            }
        });
        $('.grid-select-all').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.grid-select-all').on('ifChanged', function(event) {
            if (this.checked) {
                $('.grid-row-checkbox').iCheck('check');
            } else {
                $('.grid-row-checkbox').iCheck('uncheck');
            }
        });

        var checkRows = function () {
            var errorRows = [];
            $('.grid-row-checkbox:checked').each(function(index){
                var tr = $(this).closest('tr');
                var pSkuCode = tr.find('input[name="psku_code"]').val();
                var storageNum = tr.find('input[name="storage_num"]').val();
                if(Number(storageNum) <= 0){
                    errorRows.push(pSkuCode);
                }
            });
            return errorRows;
        };

        var selectedRows = function () {
            var param = {};
            var selected = [];
            var detail = {};
            $('.grid-row-checkbox:checked').each(function(index){
                selected.push($(this).data('id'));
                var tr = $(this).closest('tr');
                var indexArr = {};
                indexArr['detail_id'] = tr.find('input[name="detail_id"]').val();
                indexArr['psku_id'] = tr.find('input[name="psku_id"]').val();
                indexArr['psku_code'] = tr.find('input[name="psku_code"]').val();
                indexArr['storage_num'] = tr.find('input[name="storage_num"]').val();
                indexArr['pack_price'] = tr.find('input[name="pack_price"]').val();
                indexArr['unit_price_no_tax'] = tr.find('input[name="unit_price_no_tax"]').val();
                indexArr['unit_price'] = tr.find('input[name="unit_price"]').val();
                indexArr['supplier_price'] = tr.find('input[name="supplier_price"]').val();
                detail[index] = indexArr;
            });
            param.selected = selected;
            param.detail = detail;
            return param;
        };

        var getParam = function () {
            var param = {};
            param['supplier_id'] = $('input[name="supplier_id"]').val();
            param['order_no'] = $('input[name="order_no"]').val();
            return param;
        };

        $('#receiving-out-submit').on('click', function() {
            $(this).prop('disabled',true);
            var param = selectedRows();
            var ids = param.selected;
            if(ids.length == 0) {
                $(this).prop('disabled',false);
                swal('请选择出库SKU', '', 'error');
                return false;
            }
            var errorRows = checkRows();
            if(errorRows.length > 0) {
                $(this).prop('disabled',false);
                swal(errorRows.join('<br/>')+'<br/>出库数量有误', '', 'error');
                return false;
            }
            var basic = getParam();
            var detail = param.detail;
            $.ajax({
                method: 'post',
                url: '/xqc/warehousing/receive-ajax',
                data: {
                    _token:"{{ csrf_token() }}",
                    action:"purReceiveOut",
                    param:{ basic:basic,detail:detail }
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            toastr.success('提交成功 !');
                            location.href = "{{ url('xqc/warehousing/receive',[$id]) }}";
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        });
        $(".storage_num_").inputmask("integer",
            {
                onKeyValidation: function () {
                    var v = $(this).data('value');
                    var thisV = $(this).val();
                    if(thisV <= 0 || thisV > v){
                        $(this).val(v);
                    }
                }
            }
        );
    });
</script>
